﻿using System;
using System.Web;

namespace BalloonShop.App_Code
{
    /// <summary>
    /// 连接工厂类
    /// </summary>
    public class Link
    {
        //构建绝对URL
        private static string BuildAbsolute(string relativeUri)
        {
            //获取当前uri
            Uri uri = HttpContext.Current.Request.Url;
            //构建绝对路径
            string app = HttpContext.Current.Request.ApplicationPath;
            if (!app.EndsWith("/")) app += "/";
            relativeUri = relativeUri.TrimStart('/');
            //返回绝对路径
            return HttpUtility.UrlPathEncode(
                String.Format("http://{0}:{1}{2}{3}", uri.Host, uri.Port, app, relativeUri)
            );
        }

        //生成一个门类URL
        public static string ToDepartment(string departmentId, string page)
        {
            if (page == "1")
                return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID = {0}", departmentId));
            else
                return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID = {0} & Page = {1}", departmentId, page));
        }

        //为首页生成一个门类URL
        public static string ToDepartment(string departmentId)
        {
            return ToDepartment(departmentId, "1");
        }

        public static string ToCategory(string departmentId, string categoryId, string page)
        {
            if (page == "1")
                return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}", departmentId, categoryId));
            else
                return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}&Page={2}", departmentId, categoryId, page));
        }

        public static string ToCategory(string departmentId, string categoryId)
        {
            return ToCategory(departmentId, categoryId, "1");
        }

        public static string ToProduct(string productId)
        {
            return BuildAbsolute(String.Format("Product.aspx?ProductID={0}", productId));
        }

        public static string ToProductImage(string fileName)
        {
            // build product URL
            return BuildAbsolute("/ProductImages/" + fileName);
        }
    }
}